const express = require('express');
const email = express.Router();
const nodemailer = require('nodemailer');
// 获取验证码
email.get('/email', (req, res) => {
    let email = req.query.email;
    let code = Math.floor(Math.random() * 900000) + 100000;
    let transport = nodemailer.createTransport({
        host: 'smtp.qq.com',
        secureConnection: true,
        port: 465,
        auth: {
            user: 'gototao@foxmail.com',
            pass: 'vgogfpqmgxudbbhi'
        }
    })
    let emailcode = `    
    <body>
        <header style="height: 50px;">
            <h1 style="color: white;">欢迎您注册gototao账号</h1>
        </header>
        <div style="width: 1000px;margin: 0 auto;text-align: center;font-size: 30px;">
            <h1>逛淘吧</h1>
            <p>验证码：${code}</p>
        </div>
    </body>    
`
    let options = {
        from: 'gototao@foxmail.com',
        // 从前端获取到输入的邮箱
        to: email,
        subject: '欢迎注册gototao',
        html: emailcode
    };
    transport.sendMail(options, (err, msg) => {
        if (err) {
            throw err;
        } else {
            res.send({ 'msg': msg, "code": code });
            transport.close();
        }
    })
})
module.exports = email;